//分页插件
/**
2015-09-01 jason
 <div id="J_pager" class="tcdPageCode" ><!-- 这里显示分页 --></div>
**/
(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		//填充html
		fillHtml:function(obj,args){
			return (function(){
				obj.empty();
				//上一页
				if(args.current > 1){
					obj.append('<a href="javascript:;" class="prevPage">上一页</a>');
				}else{
					obj.remove('.prevPage');
					obj.append('<span class="disabled">上一页</span>');
				}
				//中间页码
				if(args.current != 1 && args.current >= 4 && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+1+'</a>');
				}
				if(args.current-2 > 2 && args.current <= args.pageCount && args.pageCount > 5){
					obj.append('<span>...</span>');
				}
				var start = args.current -2,end = args.current+2;
				if((start > 1 && args.current < 4)||args.current == 1){
					end++;
				}
				if(args.current > args.pageCount-4 && args.current >= args.pageCount){
					start--;
				}
				for (;start <= end; start++) {
					if(start <= args.pageCount && start >= 1){
						if(start != args.current){
							obj.append('<a href="javascript:;" class="tcdNumber">'+ start +'</a>');
						}else{
							obj.append('<span class="current">'+ start +'</span>');
						}
					}
				}
				if(args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5){
					obj.append('<span>...</span>');
				}
				if(args.current != args.pageCount && args.current < args.pageCount -2  && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+args.pageCount+'</a>');
				}
				//下一页
				if(args.current < args.pageCount){
					obj.append('<a href="javascript:;" class="nextPage">下一页</a>');
				}else{
					obj.remove('.nextPage');
					obj.append('<span class="disabled">下一页</span>');
				}
				obj.append('<span style="color: black;"><span style="margin: 10px;">跳转到</span><input id="txt_jason_page_jump" style="width: 50px;height: 23px;margin: 12px;"  type="text"><span  style="margin: 2px;">页</span><a  class="page-goto">确定</a> </span>');
			})();
		},
		//绑定事件
		bindEvent:function(obj,args){
		    return (function () {
		        obj.off("click");
		        obj.click(function(e) {
		            var $this = $(e.target);
		            var pageIndex = parseInt(obj.find("span.current").text());
		            if ($this.hasClass("tcdNumber")) {
		                pageTo(parseInt($this.text()));
		            } else if ($this.hasClass("prevPage")) {
		                pageTo(pageIndex - 1);
		            } else if ($this.hasClass("nextPage")) {
		                pageTo(pageIndex + 1);
		            } else if ($this.hasClass('page-goto')) {
		                var val = $('#txt_jason_page_jump').val();
		                if (!/^[1-9]+/.test(val)) {
		                    val = 1;
		                }
		                pageTo(parseInt(val));
		            }
		        });

		        function pageTo(current) {
		            if (current > args.pageCount)current = args.pageCount;
		            ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount});
		            if(typeof(args.backFn)=="function"){
		            	args.backFn(current);
		            }
		        }
		    })();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
			pageCount : 10,
			current : 1,
			backFn : function(){}
		},options);
		ms.init(this,args);
	}
})(jQuery);